package cn.sfturing.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sfturing.entity.*;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import cn.sfturing.service.DoctorService;
import cn.sfturing.service.HospitalService;
import cn.sfturing.service.OfficeService;
import cn.sfturing.utils.PageUtils;

import javax.servlet.http.HttpServletRequest;

@Controller
public class OfficeController {
	@Autowired
	private HospitalService hospitalService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private PageUtils pageUtils;
	@Autowired
	private DoctorService doctorService;

	/**
	 * 科室主界面(推荐科室)
	 * 
	 * @return
	 */
	@RequestMapping(value = "/officeIndex/{page}")
	public String officeIdex(Model model, @PathVariable("page") int page) {
		// 查询推荐的医院
		List<Hospital> hospitalRe = hospitalService.findHosByRe();
		// 设置页面
		pageUtils.setCurrentPage(page);
		pageUtils.setTotalRecord(officeService.findOfficeByReNum(hospitalRe));
		int start;
		if (pageUtils.getCurrentPage() == 0) {
			start = 0;
		} else {
			start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
		}
		Map<String, Object> officeMap = new HashMap<String, Object>();
		officeMap.put("list", hospitalRe);
		officeMap.put("start", start);
		officeMap.put("size", 20);
		List<Office> officeRe = officeService.findOfficeByRe(officeMap);
		model.addAttribute("pages", pageUtils);
		model.addAttribute("officeRe", officeRe);
		return "office/officeIndex";
	}

	/**
	 * 科室详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/officeInfoShow/{id}", method = RequestMethod.GET)
	public String hosInfoShow(Model model, @PathVariable(value = "id") int id) {
		Office office = officeService.findOfficeById(id);
		Hospital hospital = hospitalService.findHosByName(office.getHospitalName());
		List<Doctor> doctor = doctorService.findAreaByHosAndOfficeName(office.getHospitalName(),
				office.getOfficesName());
		model.addAttribute("office", office);
		model.addAttribute("hos", hospital);
		model.addAttribute("doctor", doctor);
		return "office/officeInfoShow";
	}

	/**
	 * 全部科室
	 * 
	 * @return
	 */
	@RequestMapping(value = "/orderOffice/{page}")
	public String orderOffcie(Model model, @PathVariable("page") int page, @ModelAttribute("province") String province,
			@ModelAttribute("city") String city, @ModelAttribute("district") String district, Office office) {
		// 将输入条件传回前台
		CommonCondition commonCondition = new CommonCondition();
		commonCondition.setHospitalName(office.getHospitalName());
		commonCondition.setOfficesName(office.getOfficesName());
		// 设置页面
		pageUtils.setCurrentPage(page);
		pageUtils.setTotalRecord(officeService.findOrderOfficeNum(office));
		int start;
		if (pageUtils.getCurrentPage() == 0) {
			start = 0;
		} else {
			start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
		}
		List<Office> officeRe = officeService.findOfficeByConditon(office, start, 20);
		model.addAttribute("pages", pageUtils);
		model.addAttribute("officeRe", officeRe);
		// 查询条件
		model.addAttribute("commonCondition", commonCondition);
		return "office/orderOffice";
	}

	//后台按权限查询
	@RequestMapping(value="office/list.html")
	public String getUserList(Model model, Office office, Hospital hospital,HttpServletRequest request){
		Admin admin=(Admin) request.getSession().getAttribute("adminSession");
		List<Office> officeList=new ArrayList<>();
		if(admin.getRank()>1){

			officeList = officeService.findSuper(office);
		}else {
			office.setHospitalName(admin.getHospitalName());
			officeList=officeService.selectBK(office);
		}
		model.addAttribute("office",office);
		model.addAttribute("officeList", officeList);
		return "bk/officelist";
	}

	//删除科室
	@RequestMapping(value = "/delOffice.json", method = RequestMethod.GET)
	@ResponseBody
	public Object delHouse(@RequestParam String id, HttpServletRequest request){
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(id == null || id == ""){
			resultMap.put("delResult", "notexist");
		}else{
			if(officeService.delOfficeById(Integer.parseInt(id))){
				//保存医院对应的科室
				Admin admin = (Admin)request.getSession().getAttribute("adminSession");
				List<Office> officeList=new ArrayList<>();
				if(admin.getHospitalName()!=null&&admin.getHospitalName()!=""){
					officeList = officeService.findOfficeByHosName(admin.getHospitalName());
					request.getSession().setAttribute("officeList",officeList);
				}
				resultMap.put("delResult", "true");
			}else{
				resultMap.put("delResult", "false");
			}
		}
		return JSONArray.toJSONString(resultMap);
	}

	/**
	 * 添加科室
	 */
	@RequestMapping(value = "/office/toAdd.html", method = RequestMethod.GET)
	public String toAdd() { return "bk/officeadd"; }

	/**
	 * 保存科室信息
	 */
	@RequestMapping(value = "/office/addSave.json", method = RequestMethod.POST)
	@ResponseBody
	public Object upDataSave(@RequestBody Office office, HttpServletRequest request){
		Admin admin = (Admin)request.getSession().getAttribute("adminSession");
		if (admin.getRank() < 2){
			office.setHospitalName(admin.getHospitalName());
		}
		//数据处理
		HashMap<String, String> resultMap = new HashMap<String, String>();
		office.setDoctorNum("0人");
		if(officeService.insertOffice(office)){
			//保存医院对应的科室
			List<Office> officeList=new ArrayList<>();
			if(admin.getHospitalName()!=null&&admin.getHospitalName()!=""){
				officeList = officeService.findOfficeByHosName(admin.getHospitalName());
				request.getSession().setAttribute("officeList",officeList);
			}
			resultMap.put("addResult", "true");
		}else{
			resultMap.put("addResult", "false");
		}
		return JSONArray.toJSONString(resultMap);
	}

	@RequestMapping(value = "/office/toModify.html", method = RequestMethod.GET)
	public String toModify(@RequestParam("id") Integer id, Model model) {
		Office doctor = officeService.findOfficeById(id);
		model.addAttribute("office", doctor);
		return "bk/officemodify";
	}

    /**
     * 修改科室信息
     */
    @RequestMapping(value = "/office/modifySave.json", method = RequestMethod.POST)
    @ResponseBody
    public Object upDataSave2(@RequestBody Office office){
        //数据处理
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(officeService.updataOffice(office)){
            resultMap.put("addResult", "true");
        }else{
            resultMap.put("addResult", "false");
        }
        return JSONArray.toJSONString(resultMap);
    }

	//修改科室信息
	@RequestMapping(value = "/office/toUpdata.html", method = RequestMethod.GET)
	public String toUpdata() { return "bk/officeupdata"; }

	/**
	 * 保存科室信息
	 */
	@RequestMapping(value = "/office/updataSave.json", method = RequestMethod.POST)
	@ResponseBody
	public Object UpdataSave(@RequestBody Office office){
		//数据处理
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(officeService.updateOfficeById(office)){
			resultMap.put("addResult", "true");
		}else{
			resultMap.put("addResult", "false");
		}
		return JSONArray.toJSONString(resultMap);
	}
}
